home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / package_16-january-2001.zip / Effects / Q Zfilter.cpp < prev    next >
C/C++ Source or Header  |  2000-12-10  |  4KB  |  233 lines

  1.  
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <assert.h>
  5. #include <math.h>
  6. #include "C:\progra~1\zik\buzz\dev\MachineInterface.h"
  7.  
  8. double const Silence = log(1.0 / 32768);
  9.  
  10.  
  11. CMachineParameter const parab0 = 
  12.     pt_word,                                        // type
  13.     "b0",
  14.     "Order-0 mathematical term (~DryThru)",                        // description
  15.     0,                                                // MinValue    
  16.     0xfffe,                                            // MaxValue
  17.     0xffff,                                            // NoValue
  18.     MPF_STATE,                                        // Flags
  19.     0xc000,
  20. };
  21. CMachineParameter const paraa1 = 
  22.     pt_word,                                        // type
  23.     "a1 % 1+a2",
  24.     "First order mathematical term (~Cutoff)",                        // description
  25.     0,                                                // MinValue    
  26.     0xfffe,                                            // MaxValue
  27.     0xffff,                                            // NoValue
  28.     MPF_STATE,                                        // Flags
  29.     0xfffe,
  30. };
  31. CMachineParameter const parab1 = 
  32.     pt_word,                                        // type
  33.     "b1",
  34.     "First order mathematical term (~filter mode)",                        // description
  35.     0,                                                // MinValue    
  36.     0xfffe,                                            // MaxValue
  37.     0xffff,                                            // NoValue
  38.     MPF_STATE,                                        // Flags
  39.     0x7fff,
  40. };
  41. CMachineParameter const paraa2 = 
  42.     pt_word,                                        // type
  43.     "a2",
  44.     "Second order mathematical term",                        // description
  45.     0,                                                // MinValue    
  46.     0xfffe,                                            // MaxValue
  47.     0xffff,                                            // NoValue
  48.     MPF_STATE,                                        // Flags
  49.     0x7fff,
  50. };
  51. CMachineParameter const parab2 = 
  52.     pt_word,                                        // type
  53.     "b2",
  54.     "Second order mathematical term",                        // description
  55.     0,                                                // MinValue    
  56.     0xfffe,                                            // MaxValue
  57.     0xffff,                                            // NoValue
  58.     MPF_STATE,                                        // Flags
  59.     0x4000,
  60. };
  61.  
  62. CMachineParameter const *pParameters[] = 
  63.     // global
  64.     ¶b0,
  65.     ¶a1,
  66.     ¶b1,
  67.     ¶a2,
  68.     ¶b2,
  69.     
  70. };
  71.  
  72. #pragma pack(1)        
  73.  
  74. class gvals
  75. {
  76. public:
  77.     word b0,a1,b1,a2,b2;
  78. };
  79.  
  80. #pragma pack()
  81.  
  82. CMachineInfo const MacInfo = 
  83. {
  84.     MT_EFFECT,                                // type
  85.     MI_VERSION,    
  86.     0,                                        // flags
  87.     0,                                        // min tracks
  88.     0,                                        // max tracks
  89.     5,                                        // numGlobalParameters
  90.     0,                                    // numTrackParameters
  91.     pParameters,
  92.     0,
  93.     NULL,
  94. #ifdef _DEBUG
  95.     "Q Zfilter (Debug build)",        // name
  96. #else
  97.     "Q Zfilter",                    // name
  98. #endif
  99.     "Z",                                    // short name
  100.     "Q",                        // author
  101.     NULL
  102. };
  103.  
  104.  
  105. class mi : public CMachineInterface
  106. {
  107. public:
  108.     mi();
  109.     virtual ~mi();
  110.  
  111.     virtual void Init(CMachineDataInput * const pi);
  112.     virtual void Tick();
  113.     virtual bool Work(float *psamples, int numsamples, int const mode);
  114.     virtual char const *DescribeValue(int const param, int const value);
  115.  
  116. private:
  117.     float b0,a1,b1,a2,b2;
  118.     
  119.     gvals gval;
  120.     float x1,x2,y1,y2;
  121.     
  122. };
  123.  
  124. DLL_EXPORTS
  125.  
  126. mi::mi()
  127. {
  128.     GlobalVals = &gval;
  129. }
  130.  
  131. mi::~mi()
  132. {
  133. }
  134.  
  135. void mi::Init(CMachineDataInput * const pi)
  136. {
  137.     x1=x2=y1=y2=0;
  138.     a2=0;
  139.     b2=0;
  140.     b0=1;
  141.     b1=-1;
  142.     a1=(float)0x7FFF/0x8000;
  143.  
  144. void mi::Tick()
  145. {
  146.     
  147.     if (gval.b0 != parab0.NoValue)
  148.         b0= (float)(gval.b0-0x7FFF)/0x4000;
  149.     if (gval.a1 != paraa1.NoValue)
  150.         a1= (float)(gval.a1-0x7FFF)*(1+a2)/0x8000;
  151.     if (gval.b1 != parab1.NoValue)
  152.         b1= (float)(gval.b1-0x7FFF)/0x4000;
  153.     if (gval.a2 != paraa2.NoValue)
  154.     {
  155.         a1= a1*(float)gval.a2/0x8000/(1+a2);
  156.         a2= (float)(gval.a2-0x7FFF)/0x8000;
  157.     }
  158.         
  159.     if (gval.b2 != parab2.NoValue)
  160.         b2= (float)(gval.b2-0x7FFF)/0x4000;
  161.         
  162. }
  163.  
  164. char const *mi::DescribeValue(int const param, int const value)
  165. {
  166.     static char txt[16];
  167.  
  168.     switch(param)
  169.     {
  170.     case 0:
  171.         sprintf(txt, "%.4f", b0 );
  172.         break;
  173.     case 1:
  174.         sprintf(txt, "%.4f", a1/(1+a2) );
  175.         break;
  176.     case 2:
  177.         sprintf(txt, "%.4f", b1 );
  178.         break;
  179.     case 3:
  180.         sprintf(txt, "%.4f", a2);
  181.         break;
  182.     case 4:
  183.         sprintf(txt, "%.4f", b2 );
  184.         break;
  185.     default:
  186.         return NULL;
  187.     }
  188.  
  189.     return txt;
  190. }
  191.  
  192. bool mi::Work(float *psamples, int numsamples, int const mode)
  193. {
  194.  
  195.     if (mode == WM_NOIO)
  196.         return false;
  197.     
  198.  
  199.  
  200.     do 
  201.     {
  202.         float x0=*psamples;
  203.  
  204.         if (mode & WM_WRITE)
  205.         {
  206.             *psamples=b0*x0+b1*x1+b2*x2-a1*y1-a2*y2;
  207.             y2=y1;
  208.             y1=*psamples;
  209.         }
  210.         psamples++;
  211.         x2=x1;
  212.         if (mode & WM_READ)
  213.             x1=x0;
  214.         else
  215.             x1=0;    
  216.         
  217.     } while(--numsamples);
  218.  
  219.     
  220.     if (mode & WM_WRITE)
  221.         return true;
  222.     return false;
  223. }
  224.  
  225.  
  226.